package com.hw.TimerDemo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class TimerDemo {
	
	private static Timer timer;
	private static final long period=60*60;
	
	public static void main(String[] args) throws InterruptedException {
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr1="2019-01-28 18:20:00";
		String dateStr2="2019-01-28 18:21:00";
		
		Date date1=null;
		Date date2=null;
		try {
			date1 = sdf.parse(dateStr1);
			date2 = sdf.parse(dateStr2);
			System.out.println(sdf.format(date1));
			System.out.println(sdf.format(date2));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		TimerTask job1=new TaskJob("job1");
		TimerTask job2=new TaskJob("job2");
		
		
//		如果设置Timer为守护线程，那么在jvm中没有非守护线程的时候Timer会退出，否则会一直执行
		timer=new Timer("timer 调度器实例", true);
		timer.schedule(job1, date1, period);
		timer.schedule(job2, date2, period);
		
		Thread.sleep(60*1000);
		
		
	}

}

class TaskJob extends TimerTask {

	private SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss");
	private String name;
	
	public TaskJob(String name) {
		this.name=name;
	}
	
	@Override
	public void run() {
		System.out.println("=============================="+Thread.currentThread().getName()+"开始=============================");
		System.out.println("=============================="+sdf.format(new Date())+":"+name+"=============================");
	}
	
}